EffectRT = {};
EffectRT.EffectRTContent = nil;
function EffectRT:new(...)
    return ClassUtils:extends(EffectRT, Display, ...);
end

function EffectRT:ctor(...)
    self.rawImage = nil;
    self.effectRoot = nil;
    self.camera = nil;
    self.is3D = nil;
    --通过放大rawImage的Scale值,减小size属性,来达到减小renderTexture的大小（内存）,代价就是清晰度降低
    --2d通过调整摄像机的orthographicSize属性，达到缩放粒子模型
    --3d通过拉长camera与粒子模型的距离z值,达到缩放粒子模型
    self.size = nil;
    self.renderTexture = nil;
    Display.ctor(self, ...);
end

function EffectRT:show(prefabPath, parent, sizeWH, is3D)
    if self.isInit then
        self:setVisible(true);
        return;
    end
    self.size = sizeWH;
    if not self.rawImage then
        self.rawImage = GameObject("EffectRT");
        self.rawImage.transform:SetParent(parent.transform, false);
        self.rawImage:AddComponent(typeof(RectTransform)).sizeDelta = Vector2(sizeWH, sizeWH);
        self.rawImage = self.rawImage:AddComponent(typeof(RawImage));
    end
    if not EffectRT.EffectRTContent then
        EffectRT.EffectRTContent = GameObject("EffectRTContent");
        EffectRT.EffectRTContent.transform.localPosition = Vector3(0, 0, 10000);
        --CS.UnityEngine.Object.DontDestroyOnLoad(EffectRT.EffectRTContent);
    end
    if not self.effectRoot then
        self.effectRoot = GameObject("EffectRoot");
    end
    --self.effectRoot.transform.localPosition = Vector3(EffectRT.EffectRTContent.transform.childCount*300, 0, 0);
    self.effectRoot.transform:SetParent(EffectRT.EffectRTContent.transform, false);
    for i = 0, EffectRT.EffectRTContent.transform.childCount - 1 do
        EffectRT.EffectRTContent.transform:GetChild(i).localPosition = Vector3(i * 300, 0, 0);
    end
    self:loadPrefab("Effect/" .. prefabPath, self.effectRoot);

    self:createRenderTexture();
    if is3D then
        self.is3D = true;
        self.rawImage.name = "EffectRT" .. "3D";
        self:createCamera3D();
    else
        self.is3D = false;
        self.rawImage.name = "EffectRT" .. "2D";
        self:createCamera2D();
    end

    self:setRenderTextureToRawImage();

    self.rawImage.gameObject:SetActive(self.visible);
    self.effectRoot:SetActive(self.visible);
end

function EffectRT:createCamera2D()
    if not self.camera then
        self.camera = GameObject("Camera2d"):AddComponent(typeof(Camera));
    end
    self.camera.transform:SetParent(self.effectRoot.transform, false);
    self.camera.clearFlags = CameraClearFlags.SolidColor;
    self.camera.backgroundColor = Color(0, 0, 0, 0);
    self.camera.orthographic = true;
    --2d摄像机镜头大小等于orthographicSize值乘以200
    self.camera.orthographicSize = self.size / 200;
    self.camera.nearClipPlane = 0;
    self.camera.farClipPlane = 100;
    self.camera.targetTexture = self.renderTexture;
end

function EffectRT:createCamera3D()
    if not self.camera then
        self.camera = GameObject("Camera3d"):AddComponent(typeof(Camera));
    end
    self.camera.transform:SetParent(self.effectRoot.transform, false);
    self.camera.clearFlags = CameraClearFlags.SolidColor;
    self.camera.backgroundColor = Color(0, 0, 0, 0);
    self.camera.orthographic = false;
    --3d摄像机镜头大小在fov等于90时，1单位世界大小等于100像素
    self.camera.transform.localPosition = Vector3(0, 0, self.size / -100);
    self.camera.fieldOfView = 90;
    self.camera.nearClipPlane = 0.01;
    self.camera.farClipPlane = 100;
    self.camera.targetTexture = self.renderTexture;
end

function EffectRT:createRenderTexture()
    if not self.renderTexture then
        local renderTextureFormat = RenderTextureFormat.ARGB32;
        if Application.platform == RuntimePlatform.Android or Application.platform == RuntimePlatform.IPhonePlayer then
            renderTextureFormat = RenderTextureFormat.ARGB4444;
        end
        self.renderTexture = RenderTexture(self.size, self.size, 24, renderTextureFormat);
    end
end

function EffectRT:setRenderTextureToRawImage()
    local shader = Shader.Find("Custom/UI-Default-No-Alpha");
    self.rawImage.material = Material(shader);
    self.rawImage.texture = self.renderTexture;
end

function EffectRT:onStart()
    self.gameObject.transform.localPosition = Vector3.zero;
end

function EffectRT:setVisible(visible)
    Display.setVisible(self);
    if self.rawImage then
        self.rawImage.gameObject:SetActive(self.visible);
    end
    if self.effectRoot then
        self.effectRoot:SetActive(self.visible);
    end
end

function EffectRT:setOrthographicSize(val)
    if self.camera and self.camera.orthographic then
        self.camera.orthographicSize = val;
    end
end

function EffectRT:setCameraZ(val)
    if self.camera and not self.camera.orthographic then
        self.camera.transform.localPosition = Vector3(0, 0, val);
    end
end

function EffectRT:setRawImageScale(val)
    if self.rawImage then
        self.rawImage.transform.localScale = Vector3(val, val, val);
    end
end

function EffectRT:destroy()
    if self.isInit then
        Display.destroy(self);
        Destroy(self.rawImage.gameObject);
        self.renderTexture:Release();
        Destroy(self.effectRoot);
    end
end